package bpo;

import entity.Room;
import util.EntityManagerUtil;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: RoomBpo
 * @Description:能够按照楼层号、标准查询客房详细信息，若没有输入标准，则显示指定楼层的所有客房信息
 * @Author: CHonghao
 * @Date: 2021/1/4 8:56
 * @Version: V1.0
 **/
public class RoomBpo {


    /*搜索房间通过楼层号和标准--模糊查询*/
    public static List<Map<String,String>> searchListByfs(String fno, String standard){
        EntityManager em = EntityManagerUtil.getEntityManager();
        String sql = null;
        if(standard.equals("") || standard == null){
            sql = "SELECT * FROM room r WHERE r.fno like '%"+fno+"%'";
        }else{
            sql = "SELECT * FROM room r WHERE r.fno like '%"+fno+"%' and r.standard like '%"+standard+"%'";
        }

        Query query=em.createNativeQuery(sql, Room.class);
        List<Room> Rooms = query.getResultList();
        em.close();
        List<Map<String,String>> tt = new ArrayList<Map<String,String>>();
        Map<String,String> cc = null;
        for (Room s : Rooms) {
            cc = new HashMap<String,String>();
            cc.put("rno", s.getRno());
            cc.put("fno", s.getFno());
            cc.put("standard", s.getStandard());
            cc.put("cost", String.valueOf(s.getCost()));
            cc.put("position", s.getPosition());
            tt.add(cc);
        }
        return tt;
    }
}
